﻿using System;
using System.Collections.Generic;
using UnityEngine;
using XUJson2CS.MiniJSON;

namespace XUJson2CS
{
    /**加载代理 */
    public sealed class XUJsonLoader
    {
        public delegate string LOADER_DELEGATE(string fileName);
        public static LOADER_DELEGATE loaderDelegate = null;
    }

    /**基类,接口约束 */
    public abstract class XUConfigBase
    {
        public abstract void Parse(Dictionary<string, object> json);
    }

    /**配置缓存 */
    public abstract class XUConfig<T> : XUConfigBase where T : new()
    {
        /**对象池，<表名,<id,对象>> */
        private static Dictionary<string, Dictionary<long, object>> map = new Dictionary<string, Dictionary<long, object>>();

        /**
         * 获取所有配置项
         */
        public static T[] GetAll()
        {
            /**查找结果 */
            List<T> array = new List<T>();

            /**获取配置表名称 */
            Type type = typeof(T);
            string className = type.Name;

            /**加载配置，实例化对象 */
            if (!map.ContainsKey(className))
            {
                string jsonFileName = className + ".json";
                string jsonText = XUJsonLoader.loaderDelegate(jsonFileName);
                if (string.IsNullOrEmpty(jsonText))
                {
                    Debug.LogError("加载配置失败 jsonFileName = " + jsonFileName);
                }
                else
                {
                    map.Add(className, new Dictionary<long, object>());
                    {
                        Dictionary<string, object> jsonConfig = Json.Deserialize(jsonText) as Dictionary<string, object>;

                        bool isIDValueStruct = (bool)type.InvokeMember("IsIDValueStruct", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public, null, null, null);
                        if (isIDValueStruct)
                        {
                            foreach (var jsonItem in jsonConfig)
                            {
                                if (jsonItem.Value is List<object>)
                                {
                                    Dictionary<string, object> value = new Dictionary<string, object>()
                                    {
                                        { "id", long.Parse(jsonItem.Key)},
                                        { "values" , jsonItem.Value }
                                    };
                                    XUConfigBase configItem = new T() as XUConfigBase;
                                    configItem.Parse(value);
                                    map[className].Add((long)value["id"], configItem);
                                }
                                else
                                {
                                    Dictionary<string, object> value = jsonItem.Value as Dictionary<string, object>;
                                    if (!value.ContainsKey("id"))
                                    {
                                        value.Add("id", long.Parse(jsonItem.Key));
                                    }
                                    XUConfigBase configItem = new T() as XUConfigBase;
                                    configItem.Parse(value);
                                    map[className].Add((long)value["id"], configItem);
                                }
                            }
                        }
                        else
                        {
                            XUConfigBase configItem = new T() as XUConfigBase;
                            configItem.Parse(jsonConfig);
                            map[className].Add(0, configItem);
                        }
                    }
                }
            }

            /**筛选对象 */
            if (map.ContainsKey(className))
            {
                Dictionary<long, object> pool = map[className];
                foreach (var item in pool)
                {
                    array.Add((T)item.Value);
                }
            }

            return array.ToArray();
        }
    }
}
